package com.lwj.liwanjia.view.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.EditNcBean;
import com.lwj.liwanjia.impl.EditNcView;
import com.lwj.liwanjia.presenter.EditNcPresenter;
import com.lwj.liwanjia.utils.SpfUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class EditNcActivity extends BaseActivity implements EditNcView {

    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ed_nicheng)
    EditText edNicheng;
    private String nicheng;
    private String token;
    private EditNcPresenter editNcPresenter;
    private String nick;

    @Override
    protected void initView() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("修改昵称");
        nicheng = getIntent().getStringExtra("nicheng");
        edNicheng.setText(nicheng);
        edNicheng.setSelection(nicheng.length());
        editNcPresenter = new EditNcPresenter(this);
        token = SpfUtils.getSpfUtils(this).getToken();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_nc;
    }


    @OnClick({R.id.tv_left, R.id.tv_sure})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.tv_sure://确定
                nick = edNicheng.getText().toString().trim();
                if (!nick.matches("^[\\u4E00-\\u9FA5A-Za-z0-9]{2,20}$")) {
                    Toast.makeText(this, "昵称只能由中文、英文、数字组成", Toast.LENGTH_SHORT).show();
                    return;
                }
                editNcPresenter.EditNc(token, nick);
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void editNc(EditNcBean editNcBean) {
        if (editNcBean.getState() == 0) {
            Toast.makeText(this, "修改昵称失败", Toast.LENGTH_SHORT).show();
        } else if (editNcBean.getState() == 1) {
            if (editNcBean.isData()) {
                Toast.makeText(this, "修改昵称成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.putExtra("nick", nick);
                setResult(RESULT_OK, intent);
                finish();
            }
        }
    }
}
